/**
 * 人事相关，能力评估
 */
import React from 'react';
import { history } from 'umi'
import { connect } from 'umi';
import { Form, Table, Typography } from 'antd';
import { Icon } from '@/pages/Recruitment/components';
import { EllipsisTooltip } from '@/molecules';
import { newTabProps } from '@/utils/tab';
import { TRIAL_ACTION } from '@/pages/Informal/constants';
import { FormLayout } from '../../components';
import AssessTotal from './AssessTotal';
import { AssessTag } from '.';

const getRole = (status = '') => {
    if (!status) {
        return '';
    }
    if (status.includes('Teacher')) {
        return '带教人';
    }
    if (status.includes('Leader')) {
        return '主管';
    }
    if (status.includes('HR')) {
        return '人事';
    }
    return '';
};
const map = { weekly: '周', monthly: '月', quarterly: '季' };
const AssessAbilityResult = ({ data = [], levels, employeeCode = '', user }) => {
    const opinionTitle = user.employeeCode === employeeCode ? '注意事项' : '评估意见';
    const renderOpinionTitle = v => <EllipsisTooltip>{v}</EllipsisTooltip>;
    const columns = [
        {
            title: '评估人',
            dataIndex: 'assessEmployeeName',
            width: 120,
            render: text => text || '-'
        },
        {
            title: '角色',
            dataIndex: 'period',
            width: 90,
            render: (_, row) => getRole(row.status)
        },
        {
            title: '等级',
            dataIndex: 'level',
            render(text) {
                return <AssessTag title={text} onlyText levels={levels} />;
            }
        },
        {
            title: '分数',
            dataIndex: 'score',
            render: text => (text === 0 ? text : text ?? '-')
        },
        {
            title: opinionTitle,
            dataIndex: 'opinion',
            render: text => renderOpinionTitle(text) || '-'
        },
        {
            title: '附件',
            dataIndex: 'file',
            render(file = {}) {
                const curFile = file[0] || {};
                return curFile.originName ? (
                    <Typography.Text style={{ width: 100 }} title={curFile.originName} ellipsis>
                        <a href={curFile.url}>{curFile.originName}</a>
                    </Typography.Text>
                ) : (
                    '-'
                );
            }
        },
        {
            title: '操作',
            dataIndex: 'operation',
            align: 'center',
            width: 100,
            render: (text, row) => (
                <Icon
                    title="查看详情"
                    type="listfunction_viewinterview_icon_default"
                    onClick={() => {
                        history.push({
                            pathname: '/office/personnelManagement/onboardAndLeave/informal/process/detail',
                            query: {
                                id: row.detailId
                            },
                            ...newTabProps
                        });
                    }}
                />
            )
        }
    ];

    const dataSource = React.useMemo(
        () =>
            data
                .filter(
                    item =>
                        ![
                            TRIAL_ACTION.ASSESS_HR_WEEK,
                            TRIAL_ACTION.ASSESS_HR_MONTH,
                            TRIAL_ACTION.ASSESS_HR_QUARTER
                        ].includes(item.status)
                )
                .map(item => ({ ...item, ...item.assessTable })),
        [data]
    );

    const total = React.useMemo(() => {
        const current = data.find(item => item.status.includes('Leader')) || {};
        const { level, score } = current;
        return { score, level };
    }, [data]);

    return (
        <div style={{ width: 900 }}>
            <AssessTotal data={total} levels={levels} />
            <div style={{ marginTop: 16 }} />
            <FormLayout type="full">
                <Form.Item label={`${map[dataSource?.[0]?.period] ?? ''}评估表`}>
                    <Table
                        rowKey="assessEmployeeName"
                        columns={columns}
                        dataSource={dataSource}
                        pagination={false}
                        bordered
                    />
                </Form.Item>
            </FormLayout>
        </div>
    );
};

export default connect(({ user }) => ({
    user: user.currentUser
}))(AssessAbilityResult);
